<script>
import requestUtil from "@/utils/RequestUtil";
import {Toast} from "vant";

export default {
  name: "addressList",
  data() {
    return {
      chosenAddressId: 15,
      list: [],
      disabledList: []
    }
  },
  methods: {
    init() {
      let token = this.$store.getters.getUserInfo.token
      if (!token) {
        Toast({
          message: '请先登录哦~',
          type: "fail",
          forbidClick: true,
        })
        this.$router.back()
      }
      requestUtil({
        url: '/user/address',
        method: "get",
        headers: {
          token: token,
        }
      }).then(res => {
        if (res.data.code !== 1) {
          return
        }
        let data = res.data.data
        this.list = data.map(item => {
          item['tel'] = item.phone
          item['address'] = item.province + item.region + item.detailAddress
          if (item.defaultStatus !== 'false') {
            item['isDefault'] = true
            this.chosenAddressId = item.id
          }
          return item
        })
        console.log(this.list)
      })
    },
    onEdit(item) {
      this.$router.push({
        name: 'addressManage',
        query: {
          type: 'edit',
          addressId: item.id
        }
      })
    },
    onItemClick(item) {
      let cartIds = this.$route.params.cartIds
      if (this.$route.query.source === '1'){
        this.$route.params.address = item
        // this.$router.back()
        this.$router.replace({name: 'createOrder',query: {cartIds},params: {address: item}})
      }
    }
  },
  mounted() {
    this.init()
  },
}
</script>

<template>
  <div>
    <van-address-list
        v-model="chosenAddressId"
        :disabled-list="disabledList"
        :list="list"
        default-tag-text="默认"
        disabled-text="以下地址超出配送范围"
        @add="$router.push({name: 'addressManage', query: {type: 'add'}})"
        @edit="onEdit"
        @click-item="onItemClick"
    />
  </div>
</template>

<style scoped>

</style>